package com.fsquare.controller.contact;

import java.util.HashMap;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.broadleafcommerce.common.email.dao.EmailReportingDao;
import org.broadleafcommerce.common.email.domain.EmailTarget;
import org.broadleafcommerce.common.email.service.EmailService;
import org.broadleafcommerce.common.email.service.info.EmailInfo;
import org.broadleafcommerce.common.web.controller.BroadleafAbstractController;
import org.broadleafcommerce.profile.core.domain.Customer;
import org.broadleafcommerce.profile.web.core.CustomerState;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.fsquare.component.captcha.ReCaptchaProcessor;

@Controller
@RequestMapping("/contact")
public class ContactController extends BroadleafAbstractController{
	
	private static final Log LOG = LogFactory.getLog(ContactController.class);
	
	protected static String contactView = "contact/contact";
	
	@Resource(name = "blEmailService")
    protected EmailService emailService;
	
	@Resource(name = "blEmailReportingDao")
    protected EmailReportingDao emailReportingDao;
	
	@Resource(name="blContactValidator")
    protected ContactValidator contactValidator;
	
	@Resource(name="blReCaptchaProcessor")
    protected ReCaptchaProcessor reCaptchaProcessor;
	
	
	
	@RequestMapping("")
    public String contact(HttpServletRequest request, HttpServletResponse response, Model model, @ModelAttribute("contactForm") ContactForm contactForm) {
		Customer customer = CustomerState.getCustomer();
		
		if(!customer.isAnonymous()){
			contactForm.setEmailAddress(customer.getEmailAddress());
			contactForm.setName(customer.getFirstName()+" "+customer.getLastName());
		}		
        return getContactView();
    }

	@RequestMapping(value="/sendMessage", method=RequestMethod.POST)
    public String sendMessage(HttpServletRequest request, HttpServletResponse response, Model model, @ModelAttribute("contactForm") ContactForm contactForm, BindingResult errors) {
		
//		contactValidator.validate(contactForm, errors, reCaptchaProcessor.test(request));
		reCaptchaProcessor.test(request, errors);
		if (!errors.hasErrors()) {
			EmailInfo emailInfo= new EmailInfo();
			EmailTarget emailTarget = emailReportingDao.createTarget();
			emailService.sendBasicEmail(emailInfo, emailTarget, new HashMap<String, Object>());
		}
		
        return getContactView();
    }
	

	public static String getContactView() {
		return contactView;
	}

	public EmailService getEmailService() {
		return emailService;
	}

	public void setEmailService(EmailService emailService) {
		this.emailService = emailService;
	}

	public EmailReportingDao getEmailReportingDao() {
		return emailReportingDao;
	}

	public void setEmailReportingDao(EmailReportingDao emailReportingDao) {
		this.emailReportingDao = emailReportingDao;
	}
}
